$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Audit/getDatas",
		paramFunc: paramFunc,
		operation: {
			"edit": [ShowEdit,"编辑"],
			"icon-ok":[Pass,"通过审核"],
			"icon-ok1":[kaohePass,"仅考核通过审核"],
			"icon-remove": [Refused, "拒绝审核"],	
			"icon-ok2":[convert,"转为成果奖励"],
			"icon-trash": [deleteModule, "删除"]
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

});

var paramFunc = function() {
	var data = getFormJson();
	return data;
};

var afterFunc = function(){
	$("#table tbody tr").each(function() {
		var data = $("#table").getOpts().datas[$(this).index()];
		if (data.state == 0) {
			$(this).find('#icon-trash').hide();
			$(this).find("#icon-ok2").hide();
			if (data.can_audit) {
				$(this).find('#icon-ok').show();
				$(this).find('#icon-remove').show();
				if(data.is_submit_cg == 1 && data.node == 2){
					$(this).find("#icon-ok1").show();
				}else{
					$(this).find("#icon-ok1").hide();
				}	
			} else {
				$(this).find('#icon-ok').hide();
				$(this).find('#icon-remove').hide();
				$(this).find("#icon-ok1").hide();
			}
		}
		else if (data.state == 1) {
			$(this).find('#icon-ok').hide();
			$(this).find('#icon-remove').hide();
			$(this).find('#icon-trash').hide();
			$(this).find("#icon-ok1").hide();
			if(data.is_submit_cg == 0 && data.node == 2){
				$(this).find("#icon-ok2").show();
			}else{
				$(this).find("#icon-ok2").hide();
			}
		} else { 
			$(this).find('#icon-ok').hide();
			$(this).find('#icon-remove').hide();
			$(this).find("#icon-ok1").hide();
			$(this).find("#icon-ok2").hide();
		}

		if(data.is_delete){
			$(this).find('#icon-trash').show();
		}
	});
}

var Pass = function(idx, trData){
	swal({
		title: "请确认",
		text: '是否确认通过审核',
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.post(getRootPath()+'/Admin/Audit/pass', {
					type:trData.type,
					audit_id:trData.data_id
				},  
					function(data) {
						if(data.status){
							window.parent.topMessagePrompt('success',data.msg);
				            window.parent.refreshTable();
						}else{
							window.parent.topMessagePrompt('error',data.msg);
						}
					
				},'json');	
		}
	});
	
}

var Refused = function(idx, trData){
	showDialogModalConfirm(
        '',
        '请输入拒绝原因',
        '<p>原因:</p><textarea id="remark" rows="5" style="width:100%"></textarea>',
        '确认拒绝',
        '取消',
        function() {            
           	var remark=$("#remark").val();
           	if(remark == ""){
				window.parent.topMessagePrompt('error', "请输入拒绝原因");
				return;
			}else{
				$.post(getRootPath()+'/Admin/Audit/refused', {
   					type:trData.type,
   					audit_id:trData.data_id,
   					remark:remark
   				}, 
   					function(data) {
   						if(data.status){
   							window.parent.topMessagePrompt('success',data.msg);
   				            window.parent.refreshTable();
   						}else{
   							window.parent.topMessagePrompt('error',data.msg);
   						}
   					
   				},'json');	
			}                     
        },
        function() {
            hideDialogModal();
        })
	
	
}

var deleteModule = function (idx, trData) {
	console.log(trData)
	var msg = "是否确认删除【" + trData.data_name1 + "】?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function (isConfirm) {
		if (isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Audit/delete/" + trData.id,
				async: true,
				success: function (data) {
					var ret = $.parseJSON(data);
					if (ret.status) {
						window.parent.topMessagePrompt('success', ret.msg);
						window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error', ret.msg);
					}
				}
			});
		}
	});
}

var ShowEdit=function(idx, trData){
	showDialogModal(getRootPath()+trData.show_edit);
}

//仅考核通过
var kaohePass = function(idx, trData){
	swal({
		title: "请确认仅考核通过审核",
		text: '此操作,将会把成果奖励转化为考核并审核通过进入考核库,在成果搜索中将不会出现该成果!',
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.post(getRootPath()+'/Admin/Audit/pass', {
					type:trData.type,
					audit_id:trData.data_id,
					is_kaohe:1
				},  
					function(data) {
						if(data.status){
							window.parent.topMessagePrompt('success',data.msg);
				            window.parent.refreshTable();
						}else{
							window.parent.topMessagePrompt('error',data.msg);
						}
					
				},'json');	
		}
	});
}

//审核通过的考核转化为成果奖励
var convert = function(idx, trData){
	swal({
		title: "请确认",
		text: '此操作,将会把考核数据转化为成果奖励数据',
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.post(getRootPath()+'/Admin/Audit/convert', {
					type:trData.type,
					audit_id:trData.data_id
				},  
					function(data) {
						if(data.status){
							window.parent.topMessagePrompt('success',data.msg);
				            window.parent.refreshTable();
						}else{
							window.parent.topMessagePrompt('error',data.msg);
						}
					
				},'json');	
		}
	});

}


function exportData() {
	var load_alert = loadAlert('数据统计中...');
	$.ajax({
		type: "post",
		url: "Admin/Audit/getDatas/isExport/true",
		async: true,
		dataType: 'json',
		data: $('#searchForm').serialize(),
		success: function (data) {
			closeAlert(load_alert);
			if (data.status) {
				window.location.href = data.v;
			} else {
				window.parent.topMessagePrompt('error', data.msg);
			}
		}
	});
}